import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


/**
 * @method
 * @description vuex持久化插件
 * @param {Store} store store对象
 */
const storePersist = (store) => {
  const key = 'Vuex_Persist'
  const storage = window.sessionStorage

  const data = storage.getItem(key)
  data && store.replaceState(JSON.parse(data))

  store.subscribe((mutation, store) => {
    storage.setItem(key, JSON.stringify(store))
  })
}

export default new Vuex.Store({
  state: {
    /**
     * 用户数据
     */
    user: {}

  },
  getters: {
    /**
     * 菜单
     */
    tree(state) {
      return state.user.tree
    },
    /**
     * token
     */
    token(state) {
      return state.user.token
    }
  },
  mutations: {
    /**
     * 设置用户
     */
    setUser(state, payload) {
      state.user = payload
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [storePersist]
})
